package com.xja.book.controller;

import com.xja.book.entity.BookType;
import com.xja.book.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("book")
public class BookController {
    @Autowired
    private BookTypeService bookTypeService;


    @RequestMapping("bookTypeList")
    public String typeList(Model model){
        try{
            List<BookType> typeList = bookTypeService.findBookTypes();
            model.addAttribute("typeList",typeList);
            return "type_list";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("addBookType")
    public String addBookType(BookType type){
        try{
            bookTypeService.addBookType(type);
            return "redirect:/book/bookTypeList";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("deleteBookType")
    public String deleteBookType(Integer typeId){
        try{
            bookTypeService.deleteBookType(typeId);
            return "redirect:/book/bookTypeList";
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "redirect:/error.jsp";
    }
}
